package main

import (
	"flag"
	"fmt"
	"grab/buildrequest"
	"grab/capture"
	"grab/capturesave"
	"grab/describe"
	"grab/filtrate"
	"grab/readpcap"
	"grab/reveal"
	"grab/seminar"
	"grab/swiftdecode"
	"log"

	"github.com/google/gopacket/pcap"
)

var (
	method   = flag.String("method", "getAllDevices", "输入操作方法")
	pcapFile = flag.String("pcap", "g2022-12-08_17:30:04.pcap", "pcap包,当需要读取pcap时使用")
	filter   = flag.String("filter", "tcp and port 80", "根据条件过滤协议和端口")
	device   = flag.String("device", "wlan0", "请输入监控设备")
)

func main() {

	flag.Parse()
	if *method == "" {
		panic("method必须输入")
	}

	switch *method {
	//获取本机器所有网络设备
	case "getAllDevices":
		getAllDevices()
	//实时抓取
	case "capture":
		capture.Scratch(*device)
	//抓取并保存
	case "capturesave":
		capturesave.Save(*device)
	//根据条件实时抓取
	case "filtrate":
		filtrate.Pick(*device, *filter)
	//读取
	case "readpcap":
		readpcap.Read(*pcapFile)
	//解包
	case "describe":
		describe.Explain(*device)
	//构建包
	case "buildrequest":
		buildrequest.Build(*device)
	//解析包
	case "reveal":
		reveal.Reveal()
	//自定义包
	case "seminar":
		seminar.Intangible()
	//更快的解包
	case "swiftdecode":
		swiftdecode.Interpret(*device)
	default:
	}

}

// 获取本机器所有网络设备
func getAllDevices() {
	devices, err := pcap.FindAllDevs()
	if err != nil {
		log.Fatal(err)
	}
	for _, device := range devices {
		fmt.Printf("设备名称:%v,描述:%s,地址:", device.Name, device.Description)
		for _, address := range device.Addresses {
			fmt.Printf("IP:%s,掩码:%s\n", address.IP, address.Netmask)
		}
		fmt.Println()
	}
}
